package com.voicechat.firstmet.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.activity.SendPostActivity;
import com.voicechat.firstmet.base.BaseFragment;
import com.voicechat.firstmet.utils.AudioRecorderUtil;
import com.voicechat.firstmet.utils.PopUtils;
import com.voicechat.firstmet.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.fragment_shar_girls
 * @ClassName: IMChatSendVoiceFragment
 * @Description: 聊天页面发送语音
 * @Author: ljp
 * @CreateDate: 2018/11/10 13:53
 * @Version: 1.0
 */
public class SendPostVoiceFragment extends BaseFragment {
    @BindView(R.id.tv_recording)
    TextView tvRecording;
    @BindView(R.id.image_open)
    ImageView imageOpen;
    Unbinder unbinder;
    View viewPopu;
    private SendPostActivity sendPostActivity;

    @Override
    protected int loadLayout() {
        return R.layout.fragment_send_voice;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        initView();
    }

    private void initView() {
        sendPostActivity = (SendPostActivity) activity;
        viewPopu = View.inflate(context, R.layout.popu_voice_introduce, null);
        imageOpen.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int start_x = 0, start_y = 0, end_x, end_y, mov_x, mov_y;
                // 处理动作
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        //开始录制  显示倒计时页面
                        sendPostActivity.startSendVoice();
                        imageOpen.setImageResource(R.mipmap.record_down);
                        //recordTimerUtil.start();
                        tvRecording.setText("上滑取消");
                        showRecordVoicePopu(0);
                        break;
                    case MotionEvent.ACTION_UP:
                        if (PopUtils.isPopShowing()) {
                            PopUtils.hidePopupWindow();
                        }
                        tvRecording.setText("长按录制");
                        imageOpen.setImageResource(R.mipmap.record);
                        end_x = (int) event.getX();
                        end_y = (int) event.getY();
                        mov_x = Math.abs(start_x - end_x);
                        mov_y = Math.abs(start_y - end_y);
                        if (mov_x > 200 || mov_y > 200) {//取消录音
                            sendPostActivity.cacalSendVoice();
                        }else {
                            sendPostActivity.endSendVoice();
                        }
                    case MotionEvent.ACTION_CANCEL:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        end_x = (int) event.getX();
                        end_y = (int) event.getY();
                        mov_x = Math.abs(start_x - end_x);
                        mov_y = Math.abs(start_y - end_y);
                        if (mov_x > 200 || mov_y > 200) {
                            showRecordVoicePopu(1);
                        } else if (mov_x < 200 || mov_y < 200) {
                            showRecordVoicePopu(0);
                        }
                        break;
                }
                return true;
            }
        });
    }

    //pop 的录制中动画图片
    ImageView imageRecording;
    //pop  录制中显示布局
    LinearLayout linearRecordOn;
    //pop  上滑时显示布局
    LinearLayout linearRecordUp;
    /**
     * @return flay判断是上滑还是长按录制中
     * @method
     * @description 按住上滑弹出popu布局
     * @date: 2018/9/28 19:11
     * @author: ljp
     */
    private void showRecordVoicePopu(int flag) {
        imageRecording = viewPopu.findViewById(R.id.image_recording);
        linearRecordOn = viewPopu.findViewById(R.id.linear_record_on);
        ImageView imageRecordCacel = viewPopu.findViewById(R.id.image_record_cacel);
        linearRecordUp = viewPopu.findViewById(R.id.linear_record_up);
        if (!PopUtils.isPopShowing()) {
            PopUtils.showPopupWindowRecprd(activity, viewPopu, true);
            linearRecordOn.setVisibility(View.VISIBLE);
        } else {
            if (flag == 0) {//按下时布局
                linearRecordOn.setVisibility(View.VISIBLE);
                linearRecordUp.setVisibility(View.GONE);
            } else {
                linearRecordOn.setVisibility(View.GONE);
                linearRecordUp.setVisibility(View.VISIBLE);
            }
        }
        sendPostActivity.getRecorder().setOnAudioStatusUpdateListener(new AudioRecorderUtil.OnAudioStatusUpdateListener() {
            @Override
            public void onStart() {
            }
            @Override
            public void onProgress(double db, long time) {
                imageRecording.setImageLevel((int) db);
              //  Log.i("SendPostVoiceFragment", "onProgress: ----------"+Integer.parseInt(Utils.long2String(time)));
                if (Integer.parseInt(Utils.long2String(time)) >= 61) {
                    sendPostActivity.getRecorder().stop();
                    sendPostActivity.endSendVoice();
                    PopUtils.hidePopupWindow();
                }
            }
            @Override
            public void onError(Exception var1) {
            }
            @Override
            public void onCancel() {
            }
            @Override
            public void onStop(String var1) {
                if (PopUtils.isPopShowing()) {
                    PopUtils.hidePopupWindow();
                }
            }
        });


    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment_shar_girls view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.image_open)
    public void onClick() {
    }
}
